package khs.java_apache_httpclient.main.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException
    {
        String response = "";
        HttpClientContext context = null;
        
        System.out.print("input url: ");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String input = bufferedReader.readLine();
        
        System.out.print("userid: ");
        String userid = bufferedReader.readLine();
        
        CloseableHttpClient httpClient = HttpClients.createDefault();
        
        
        if(!userid.isEmpty()){
            System.out.print("password: ");
            String password = bufferedReader.readLine();
            
            URL url = new URL(input);
            
            HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userid, password));
            AuthCache authCache = new BasicAuthCache();
            DigestScheme digestAuth = new DigestScheme();
            authCache.put(targetHost, digestAuth);
            
            context = HttpClientContext.create();
            context.setCredentialsProvider(credsProvider);
            context.setAuthCache(authCache);
        }
        
        HttpGet httpGet = new HttpGet(input);
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet, context);
        HttpEntity entity = httpResponse.getEntity();
        
        StatusLine statusLine = httpResponse.getStatusLine();
        
        // 응답코드
        int statusCode = statusLine.getStatusCode();
        System.out.println(">>> statusCode: " + statusCode);
        
        if(entity != null){
            long contentLength = entity.getContentLength();
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            
            String line = "";
            while((line = rd.readLine()) != null){
                response += line;
            }
        }
        
        httpResponse.close();
        httpGet.abort();
        System.out.println(response);
    }
}
